<?php
/**
 * Created by PhpStorm.
 * User: jhon
 * Date: 2018/2/9
 * Time: 9:35
 */

namespace app\admin\controller;
use think\Request;
use think\Db;

class Org extends Auto
{

    public function index(){
        return $this->fetch();
    }
//    添加机构
    public function orgadd(){
        $Request = Request::instance();
        $param = $Request->param();
        $data = [
            'org_code'=>$this::getorgcode(),
            'org_name'=>$param['org_name'],
            'org_region'=>$param['org_region'],
            'org_address'=>$param['org_address'],
            'org_desc'=>$param['org_desc'],
            'post_code'=>$param['post_code'],

            'org_grade'=>session('org.org_grade')+1,
            'org_parent_code'=>session('user.org_code'),
            'org_parent_name'=>session('org.org_name'),

            'contacts_name'=>$param['contacts_name'],
            'contacts_duties'=>$param['contacts_duties'],
            'contacts_moblie'=>$param['contacts_moblie'],
            'contacts_sex'=>$param['contacts_sex'],

            'mark'=>1,
            'create_date'=>date('Y-m-d H:i:s',time()),
        ];
//        var_dump($data);
//        exit();
        $result  = Db::table('organization')->insert($data);

        if($result){
            $resultId = Db::table('organization')->getLastInsID();
            return $resultId;
//            var_dump($resultId);
        }else{//添加失败
            return 0;
        }

    }
//    修改机构
    public function orgupd(){
        $Request = Request::instance();
        $param = $Request->param();
//        var_dump($param);
        $data = [
            'org_name'=>$param['org_name'],
            'post_code'=>$param['post_code'],
            'mark'=>$param['mark'],
            'org_region'=>$param['org_region'],
            'org_address'=>$param['org_address'],
            'org_desc'=>$param['org_desc'],

            'contacts_name'=>$param['contacts_name'],
            'contacts_duties'=>$param['contacts_duties'],
            'contacts_moblie'=>$param['contacts_moblie'],
            'contacts_sex'=>$param['contacts_sex'],
        ];
        $ID = $param['org_sn'];
        $result  = Db::table('organization')->where('org_sn',$ID)->update($data);
        echo $result;
    }
//    删除机构
    public function orgdel(){
        $Request = Request::instance();
        $param = $Request->param();
        if($param['ID'] == 1){
            return "error";
        }
        $ID = $param['ID'];
        $result  = Db::table('organization')->delete($ID);
        echo $result;
    }
//    查询机构列表
    public function orgquery(){
        $result  = Db::table('organization')
            ->where('org_parent_code',session('user.org_code'))
            ->select();
        $dataarr = [
            'data'=>$result
        ];
        echo json_encode($dataarr);
    }
//    查询机构详情
    public function orgsel(){
        $Request = Request::instance();
        $param = $Request->param();
        $ID = $param['ID'];

        $result  = Db::table('organization')->where('org_sn',$ID)->select();
//        var_dump($result[0]);
        echo json_encode($result[0]);
    }

    static function getorgcode(){
        return 'ORG_'.uniqid();
    }

}